package com.swz.dcrm.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.swz.dcrm.R;
import com.swz.dcrm.adpter.MultipleChoiceAdapter;
import com.swz.dcrm.model.MultipleChoice;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.callback.OnClickListener;
import com.xh.baselibrary.util.AnimationUtil;


import java.util.List;

import ezy.ui.view.RoundButton;

public class MultipleChoiceView<T> extends PopupWindow {

    private RecyclerView rv;
    private TextView tvTitle;
    private RoundButton confirm;
    private MultipleChoiceAdapter multiItemTypeAdapter;
    private OnClickListener<List<T>> onClickListener;
    private SmartRefreshLayout smartRefreshLayout;
    OnLoadmoreListener onLoadmoreListener;

    public   MultipleChoiceView(Context context,String title,List<MultipleChoice<T>> list,String btnText){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_multiple_choice,null,false);
        rv=view.findViewById(R.id.rv);
        tvTitle=view.findViewById(R.id.tv_title);
        confirm=view.findViewById(R.id.btn_confirm);
        tvTitle.setText(title);
        confirm.setText(btnText);
        multiItemTypeAdapter= new MultipleChoiceAdapter(context, list,true) {
            @Override
            public List<String> addHasCheckedList() {
                return null;
            }
        };
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.addItemDecoration(new CustomDecoration(context,0,5,15,15));
        rv.setAdapter(multiItemTypeAdapter);
        setOutsideTouchable(true);
        setAnimationStyle(R.style.showPopupAnimation);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        confirm.setOnClickListener(v -> {
            dismiss();
            if(onClickListener!=null){
                onClickListener.onItemClick(multiItemTypeAdapter.getCheckedList());
            }
        });
        setContentView(view);
    }
    public   MultipleChoiceView(Context context,String title,List<MultipleChoice<T>> list,String btnText,List<String>hasChecked){
        View view = LayoutInflater.from(context).inflate(R.layout.layout_multiple_choice,null,false);
        smartRefreshLayout=view.findViewById(R.id.smartRefreshLayout);
        smartRefreshLayout.setEnableLoadmore(false);
        if(onLoadmoreListener!=null){
            smartRefreshLayout.setOnLoadmoreListener(onLoadmoreListener);
        }
        rv=view.findViewById(R.id.rv);
        tvTitle=view.findViewById(R.id.tv_title);
        confirm=view.findViewById(R.id.btn_confirm);
        tvTitle.setText(title);
        confirm.setText(btnText);
        multiItemTypeAdapter= new MultipleChoiceAdapter(context, list,false) {
            @Override
            public List<String> addHasCheckedList() {
                return hasChecked;
            }
        };
        rv.setLayoutManager(new LinearLayoutManager(context));
        rv.addItemDecoration(new CustomDecoration(context,0,5,15,15));
        rv.setAdapter(multiItemTypeAdapter);
        setOutsideTouchable(true);
        setAnimationStyle(R.style.showPopupAnimation);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        confirm.setOnClickListener(v -> {
            dismiss();
            if(onClickListener!=null){
                onClickListener.onItemClick(multiItemTypeAdapter.getCheckedList());
            }
        });

        setContentView(view);

    }

    public void setOnClickListener(OnClickListener<List<T>> onClickListener) {
        this.onClickListener = onClickListener;
    }

    public MultipleChoiceView setTitle(String t){
        tvTitle.setText(t);
        return this;
    }

    public void refresh(int pageNum,List<MultipleChoice<T>>list){
        multiItemTypeAdapter.refresh(pageNum,list);
    }
    public void setOnLoadmoreListener(OnLoadmoreListener onLoadmoreListener) {
        this.onLoadmoreListener = onLoadmoreListener;
    }

    public MultipleChoiceAdapter getMultiItemTypeAdapter() {
        return multiItemTypeAdapter;
    }
}
